package bank;

public class _35SearchInsert {
    public static int searchInsert(int[] nums, int target) {
        if (target > nums[nums.length - 1]){
            return nums.length;
        }else if (target < nums[0]){
            return 0;
        }
        //  二分法查找
        int left = 0,right = nums.length - 1;
        while (left < right - 1){
            int mid = (left + right) >> 1;
            if (nums[mid] == target){
                return mid;
            }else if (nums[mid] > target){
                //  往左找
                right = mid;
            }else {
                //  往右找
                left = mid;
            }

        }
        if (nums[left] == target){
            return left;
        }else{
            return right;
        }
    }
}
